//
//  ViewController.m
//  ChangeLocation
//
//  Created by ljb48229 on 2018/1/26.
//  Copyright © 2018年 ljb48229. All rights reserved.
//

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
#import "ChangeLoction.h"
#import "ChangeLocation-Swift.h"


@interface ViewController ()<CLLocationManagerDelegate>

@property(nonatomic ,strong) CLLocationManager *manager;
@property (weak, nonatomic) IBOutlet UILabel *label0;
@property (strong, nonatomic) UILabel *location2DLable;
@property (strong, nonatomic) UILabel *locationLable;
@property(nonatomic, strong) NSString *locaton;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UITapGestureRecognizer * gesture = [[UITapGestureRecognizer alloc]init];
    [gesture addTarget:self action:@selector(viewClick)];
    [self.location2DLable addGestureRecognizer:gesture];
    self.location2DLable.userInteractionEnabled = YES;
    //如果要想知道任意位置的坐标
    //1.去高德地图http://lbs.amap.com/console/show/picker，选中自己坐标
    //杭州  120.211963,30.274602
    //恒泰中心  39.866242,116.306313
    //
    //奥北中心北区  104.063669,30.539589
    //2.在进行坐标转换
    
    CLLocationCoordinate2D location2D = CLLocationCoordinate2DMake(104.245862,30.710892);
    CLLocationCoordinate2D WGSlocation2D = [ChangeLoction gcj02ToWgs84:location2D];
    NSLog(@"转换后的：纬度：%f,经度：%f",WGSlocation2D.latitude , WGSlocation2D.longitude);
    
    //纬度：30.277029,经度：120.207428
    //3.去Location1.gpx修改经纬度
//    [FKSwift downloadURLWithUrls:@[@"https://www.baidu.com",@"https://www.baidu.com"] completion:^(NSArray<NSString *> * _Nonnull texts) {
//        NSLog(@"%@",texts);
//    }];
//    [FKSwift imageWithUrl:@"https://cn.bing.com/" completion:^(UIImage * _Nullable image) {
//        NSLog(@"image -- %@",image);
//    }];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    _manager = [[CLLocationManager alloc] init];
    _manager.delegate = self;
//    [manager requestAlwaysAuthorization];
    [_manager requestWhenInUseAuthorization];
    _manager.desiredAccuracy = kCLLocationAccuracyBest;
    _manager.distanceFilter = 1.0;
    [_manager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    
    CLLocation *currentLocation = [locations lastObject];
    //1.如果打卡地点是这里，当前的经纬度。
    //2.直接copy下面的经纬度，无需转换
    //3.去Location1.gpx修改经纬度
    //当前的经纬度
    NSLog(@"当前的经纬度 %f,%f",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude);
    self.location2DLable.text = [NSString stringWithFormat:@"当前  纬度:%f,经度:%f,海拔:%f",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude,currentLocation.altitude];
    
    //地理反编码 可以根据坐标(经纬度)确定位置信息(街道 门牌等)
    CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
    [geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if (placemarks.count >0) {
            CLPlacemark *placeMark = placemarks[0];
            NSString *currentCity = placeMark.locality;
            if (!currentCity) {
                currentCity = @"无法定位当前城市";
            }
            //看需求定义一个全局变量来接收赋值
//            NSLog(@"当前国家 - %@",placeMark.country);//当前国家
//            NSLog(@"当前城市 - %@",currentCity);//当前城市
//            NSLog(@"当前位置 - %@",placeMark.subLocality);//当前位置
//            NSLog(@"当前街道 - %@",placeMark.thoroughfare);//当前街道
//            NSLog(@"具体地址 - %@",placeMark.name);//具体地址
           NSMutableString * message = [NSMutableString stringWithFormat:@"%@,%@,%@,%@,%@",placeMark.country,currentCity,placeMark.subLocality,placeMark.thoroughfare,placeMark.name];
            if (@available(iOS 15.0, *)) {
                [message appendFormat:@"\n是否是虚拟定位：%d", currentLocation.sourceInformation.isSimulatedBySoftware];
                [message appendFormat:@"\n是否是外接设备虚拟定位：%d", currentLocation.sourceInformation.isProducedByAccessory];
            }
            self.locationLable.text = message;
            self.locaton = [NSString stringWithFormat:@"%@,%@,%@,%@,%@\n当前经纬度：%f,%f,%f ",placeMark.country,currentCity,placeMark.subLocality,placeMark.thoroughfare,placeMark.name,currentLocation.coordinate.latitude,currentLocation.coordinate.longitude,currentLocation.altitude];
        }
    }];
}
    
- (UILabel *)location2DLable{
    if (!_location2DLable) {
        _location2DLable = [[UILabel alloc]init];
        _location2DLable.textAlignment = NSTextAlignmentCenter;
        _location2DLable.numberOfLines = 0;
        _location2DLable.textColor = UIColor.blackColor;
        [self.view addSubview:_location2DLable];
        [_location2DLable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(20);
            make.right.mas_offset(-20);
            make.centerY.equalTo(self.view.mas_centerY);
        }];
    }
    return _location2DLable;
}

- (UILabel *)locationLable{
    if (!_locationLable) {
        _locationLable = [[UILabel alloc]init];
        _locationLable.textAlignment = NSTextAlignmentCenter;
        _locationLable.numberOfLines = 0;
        _locationLable.textColor = UIColor.blackColor;
        [self.view addSubview:_locationLable];
        [_locationLable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(20);
            make.right.mas_offset(-20);
            make.top.equalTo(self.location2DLable.mas_bottom).mas_offset(10);
        }];
    }
    return _locationLable;

}

- (void)viewClick{
    if (self.locaton.length <= 0) {
        [self.view makeToast:@"没有经纬度数据"];
        return;
    }
    
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    [pasteboard setString:self.locaton];
    [self.view makeToast:@"已复制到剪切板"];
}

/**
 
 <wpt lat="39.865161" lon="116.300313">
 <name>恒泰广场</name>
 <time>2018-10-19T08:55:37Z</time>
 </wpt>
 
 <wpt lat="39.865160" lon="116.300314">
 <name>恒泰广场</name>
 <time>2018-10-19T08:56:37Z</time>
 </wpt>
 
 */
    
    
    
@end
